/**
 * Created by .
 * User: Administrator
 * Date: 11-9-5
 * Time: 下午1:40
 * To change this template use File | Settings | File Templates.
 */
Vpa.doc.Doc2File = function(classes){
    this.classes = classes;
    this.createDoc();
};
Vpa.doc.Doc2File.prototype = {
    createDoc : function(){
        var classes = this.classes;
        for(var className in classes){
            var cls = classes[className];
            if(cls.isPrivate || cls.isIgnored || cls.isHidden)
                continue;
            var doc = this.docClass(cls);
            this.docCfg(cls,doc);
            this.docExtendInfo(cls,doc);

            var reg = /\./g;
            v8WriteFile(
                vpaConfig.outputDir+className+".js",
                "Ext.data.JsonP."+className.replace(reg,"_")+"("+JSON.stringify(doc)+")"
            );
        }
    },
    docClass : function(cls){
        var className = cls.className;

        var doc = {
            "mixedInto":[],
            "superclasses":[],
            "inheritable": false,
            "subclasses":[],
            "deprecated": null,
            "allMixins":[],
            "href" : cls.sourceHref,
            "html_filename" : cls.sourceFileName,
            "members":{
                "cfg":[],
                "method":[],
                "event":[],
                "css_var":[],
                "css_mixin":[],
                "property":[]
            },
            "singleton": cls.isSingleton,
            "protected": false,
            "tagname": "class",
            "mixins": [],
            "alias": null,
            "author": null,
            "filename": "",
            "private": cls.isPrivate,
            "alternateClassNames": [],
            "static": cls.isStatic,
            "name": className,
            "doc": vpaConfig.markdown ? Vpa.Markdown.toHTML(cls.description) : cls.description,
            "docauthor": "",
            "component": false,
            "linenr": 1,
            "xtypes": [],
            "html_filename": "ToolTip.html",
            "statics": {
                "cfg": [],
                "method": [],
                "event": [],
                "css_var": [],
                "css_mixin": [],
                "property": []
            },
            "extends": cls.superClassName
        };
        var pcls = cls,
            classes = this.classes;
        while(pcls && pcls.superClassName && pcls.superClassName.length > 0){
            doc.superclasses.push(pcls.superClassName);
            pcls = classes[pcls.superClassName];
        }
        doc.superclasses.reverse();

        return doc;
    },
    sortArray : function(arr){
        arr.sort(function(a,b){
            if(a.name > b.name)
                return 1;
            else if(a.name < b.name)
                return -1;
            else
                return 0;
        });
    },
    docCfg : function(cls, doc){
        var cfgs = cls.configs,
            className = cls.className;
        for(var i=0, len=cfgs.length;i<len;i++){
            var cfg = cfgs[i];
            var cfgItem = {
                "inheritable": false,
                "type": cfg.type,
                "deprecated": null,
                "href": cfg.href,
                "protected": false,
                "tagname": "cfg",
                "alias": null,
                "filename": "",
                "private": cfg.isPrivate,
                "shortDoc": cfg.shortDesc,
                "static": cfg.isStatic,
                "name": cfg.cfgName,
                "owner": className,
                "doc": vpaConfig.markdown ? Vpa.Markdown.toHTML(cfg.description) : cfg.description,//cfg.description,
                "linenr": 1,
                "html_filename": cfg.html_filename
            };
            if(cfg.isStatic){
                doc.statics.cfg.push(cfgItem);
            }else{
                doc.members.cfg.push(cfgItem);
            }
        }

        this.sortArray(doc.statics.cfg);
        this.sortArray(doc.members.cfg);
    },
    docExtendInfo : function(cls, doc){
        var className = cls.className;
        var pcls = {superClassName:className},
            classes = this.classes;
        while(pcls && pcls.superClassName && pcls.superClassName.length > 0){
            pcls = classes[pcls.superClassName];
            if(!pcls)
                break;
            this._docMethod(pcls, doc, className);
            this._docEvent(pcls, doc, className);
            this._docProperty(pcls, doc, className);
        }
        var sortArray = this.sortArray;
        sortArray(doc.statics.method);
        sortArray(doc.members.method);
        sortArray(doc.statics['event']);
        sortArray(doc.members['event']);
        sortArray(doc.statics['property']);
        sortArray(doc.members['property']);
    },
    _docMethod : function(cls,doc,oriClassName){
        var methods = cls.methods;
        var className = cls.className;
        if(!methods || methods.length<1)
            return;
        for(var i=0, len=methods.length;i<len;i++){
            var _method = methods[i];

            if(_method.isStatic && className != oriClassName)
                continue;
            if(_method.isPrivate || _method.isIgnored || _method.isHidden)
                continue;
            var methodItem = {
                "inheritable": false,
                "deprecated": null,
                "params": [],
                "href": _method.href,
                "return": {},
                "protected": false,
                "tagname": "method",
                "alias": null,
                "filename": "",
                "private": _method.isPrivate,
                "shortDoc": _method.shortDesc,
                "static": _method.isStatic,
                "name": _method.name,
                "owner": className,
                "doc": vpaConfig.markdown ? Vpa.Markdown.toHTML(_method.description) : _method.description,
                "linenr": 1,
                "html_filename": _method.html_filename
            }
            var params = _method.params;
            for(var j=0,plen=params.length;j<plen;j++){
                var param = params[j];
                methodItem.params.push({
                    "type": param.type,
                    "optional": param.isOptional,
                    "doc": vpaConfig.markdown ? Vpa.Markdown.toHTML(param.description) : param.description,//param.description,
                    "name": param.paramName
                });
            }

            if(_method["return"]){
                methodItem["return"] = {
                    type : _method["return"].type,
                    doc : _method["return"].description
                }
            }

            if(_method.isStatic){
                doc.statics.method.push(methodItem);
            }else{
                doc.members.method.push(methodItem);
            }
        }
    },
    _docEvent : function(cls,doc,oriClassName){
        var events = cls.events,
            className = cls.className;
        for(var i=0,len=events.length;i<len;i++){
            var _event = events[i];

            if(_event.isStatic && className != oriClassName)
                continue;
            if(_event.isPrivate || _event.isIgnored || _event.isHidden)
                continue;
            var eventItem = {
                "inheritable": false,
                "deprecated": null,
                "params": [],
                "href": _event.href,
                "protected": false,
                "tagname": "event",
                "alias": null,
                "filename": "",
                "private": _event.isPrivate,
                "shortDoc": _event.shortDesc,
                "static": _event.isStatic,
                "name": _event.eventName,
                "owner": className,
                "doc": _event.description,
                "linenr": 1,
                "html_filename": _event.html_filename
            }

            var params = _event.params;
            for(var j=0,plen=params.length;j<plen;j++){
                var param = params[j];
                eventItem.params.push({
                    "type": param.type,
                    "optional": param.isOptional,
                    "doc": param.description,
                    "name": param.paramName
                });
            }

            if(_event.isStatic){
                doc.statics["event"].push(eventItem);
            }else{
                doc.members["event"].push(eventItem);
            }
        }
    },
    _docProperty : function(cls, doc, oriClassName){
        var properties = cls.properties,
            className = cls.className;
        for(var i=0,len=properties.length;i<len;i++){
            var property = properties[i];

            if(property.isStatic && className != oriClassName)
                continue;
            if(property.isPrivate || property.isIgnored || property.isHidden)
                continue;
            var propertyItem = {
                "inheritable": false,
                "type": property.type,
                "deprecated": null,
                "href": property.href,
                "protected": false,
                "tagname": "property",
                "alias": null,
                "filename": "",
                "private": property.isPrivate,
                "shortDoc": property.shortDesc,
                "static": property.isStatic,
                "name": property.name,
                "owner": className,
                "doc": property.description,
                "linenr": 1,
                "html_filename": property.html_filename
            }

            if(property.isStatic){
                doc.statics["property"].push(propertyItem);
            }else{
                doc.members["property"].push(propertyItem);
            }
        }
    }
};
